/**
 * Copyright (c) 2009, http://www.softwareinlife.com. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, 
 * with or without modification, are permitted provided that 
 * the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice, 
 *    this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution.
 * 
 * 3. Neither the name of the http://www.softwareinlife.com nor the names of its 
 *    contributors may be used to endorse or promote products derived from 
 *    this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE SOFTWAREINLIFE AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.softwareinlife.vision.android.ux.vision;

import java.util.Date;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.softwareinlife.vision.android.R;
import com.softwareinlife.vision.android.data.dao.UserDAO;
import com.softwareinlife.vision.android.data.dao.VisionDAO;
import com.softwareinlife.vision.android.data.exceptions.DatabaseConnectionException;
import com.softwareinlife.vision.android.data.exceptions.VisionCreateException;
import com.softwareinlife.vision.android.data.model.VisionModel;
import com.softwareinlife.vision.android.ux.util.SharedPreferenceUtility;

public class CreateVisionActivity extends Activity {

	// Member Variables //////////////////////////////

	/**
	 * Title Text View
	 */
	private EditText titleEditText = null;

	/**
	 * Statement Edit Text
	 */
	private EditText statementEditText = null;

	// START DATE //////////////////////////////////

	/**
	 * Date of Start
	 */
	private int startDateYear; // Year
	private int startDateMonth; // Month
	private int startDateDay; // Day

	/**
	 * TextView of Start Date
	 */
	private TextView startDateTextView = null;

	/**
	 * Button for Start Date Picker
	 */
	private Button startDatePickerButton = null;

	/**
	 * Constants for Start Date
	 */
	protected static final int START_DATE_DIALOG_ID = 1;

	/**
	 * Click Listener for Start Date
	 */
	protected OnClickListener startDateClickListener = null;

	/**
	 * Date Select Listener for Start Date
	 */
	protected OnDateSetListener startDateSelectListener = null;

	// END DATE ///////////////////////////////////

	/**
	 * Date of End
	 */
	private int endDateYear; // Year
	private int endDateMonth; // Month
	private int endDateDay; // Day

	/**
	 * TextView of End Date
	 */
	private TextView endDateTextView = null;

	/**
	 * Button for Start Date Picker
	 */
	private Button endDatePickerButton = null;

	/**
	 * Constants for End Date
	 */
	protected static final int END_DATE_DIALOG_ID = 0;

	/**
	 * Click Listener for End Date
	 */
	protected OnClickListener endDateClickListener = null;

	/**
	 * Date Select Listener for End Date
	 */
	protected OnDateSetListener endDateSelectListener = null;

	// CREATE BUTTON /////////////////////////////////////

	/**
	 * Button of Create
	 */
	private Button createButton = null;

	// CANCEL BUTTON /////////////////////////////////////

	/**
	 * Button of Cancel
	 */
	private Button cancelButton = null;

	// BLUEPRINT //////////////////////////////////////////

	/**
	 * Blueprint Color Spinner
	 */
	private Spinner blueprintColorSpinner = null;

	/**
	 * Blueprint Color TextView
	 */
	private TextView blueprintColorTextView = null;

	/**
	 * Vision DAO
	 */
	private VisionDAO dao = null;

	/**
	 * Constructor
	 */
	public CreateVisionActivity() {
		super();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Set View
		setContentView(R.layout.vision_create);

		// Initialize Members
		initMemembers();
	}

	/**
	 * Initialize Members
	 */
	private void initMemembers() {
		// Title
		titleEditText = (EditText) findViewById(R.id.titleEditText);

		// Statement
		statementEditText = (EditText) findViewById(R.id.statmentEditText);

		// Blueprint
		blueprintColorTextView = (TextView) findViewById(R.id.blueprintColorTextView);

		// Blueprint Spinner
		blueprintColorSpinner = (Spinner) findViewById(R.id.blueprintColorSpinner);

		// Blueprint ArrayAdapter
		ArrayAdapter arrayAdapter = ArrayAdapter.createFromResource(
				getApplicationContext(), R.array.blueprint_color_arrary,
				android.R.layout.simple_spinner_item);

		arrayAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		blueprintColorSpinner.setAdapter(arrayAdapter);

		// CREATE BUTTON //////////////////////////////////
		createButton = (Button) findViewById(R.id.createButton);
		createButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				createVisionData();
				changeVisionListScreen();
			}
		});

		// CANCEL BUTTON //////////////////////////////////
		cancelButton = (Button) findViewById(R.id.cancelButton);
		cancelButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				changeVisionListScreen();
			}
		});
	}

	/**
	 * Change Vision List Screen
	 */
	protected void changeVisionListScreen() {
		Intent intent = new Intent(this, ReadVisionActivity.class);
		startActivity(intent);
	}

	/**
	 * Create Vision Data
	 */
	protected void createVisionData() {
		try {
			// Create Model
			VisionModel model = new VisionModel();
			model.setTitle(titleEditText.getText().toString());
			model.setStatement(statementEditText.getText().toString());
			model.setBlueprint(blueprintColorSpinner.getSelectedItem()
					.toString());
			
			// Get User ID
			String userId = SharedPreferenceUtility.getUserId(getApplicationContext());
			Log.d("USER_ID", userId);

			// Check User Id
			if (userId != null && userId.length() > 0) {
				UserDAO userDAO = new UserDAO(getApplicationContext());
				model.setUser(userDAO.read(userId));
				userDAO.close();

				// Now
				Date now = new Date();
				model.setCreatedDate(now);
				model.setModifiedDate(now);

				// Create DAO
				dao = new VisionDAO(getApplicationContext());

				// Create Operation
				dao.create(model);
			}
		} catch (DatabaseConnectionException dce) {
			dce.printStackTrace();

		} catch (VisionCreateException vce) {
			vce.printStackTrace();

		}
	}

	/**
	 * Display Start Date
	 */
	/*
	protected void updateStartDateDisplay() {
		// StrinbBuilder
		StringBuilder sb = new StringBuilder();
		sb.append(startDateYear);
		sb.append("-");
		sb.append(startDateMonth + 1);
		sb.append("-");
		sb.append(startDateDay);

		startDateTextView.setText(sb);
	}
	*/
	/**
	 * Display End Date
	 */
	/*
	protected void updateEndDateDisplay() {
		// StrinbBuilder
		StringBuilder sb = new StringBuilder();
		sb.append(endDateYear);
		sb.append("-");
		sb.append(endDateMonth + 1);
		sb.append("-");
		sb.append(endDateDay);

		endDateTextView.setText(sb);
	}
	*/
	/*
	@Override
	
	protected Dialog onCreateDialog(int id) {
		Dialog dialog = null;

		switch (id) {

		// Start Date
		case START_DATE_DIALOG_ID:
			dialog = new DatePickerDialog(this, startDateSelectListener,
					startDateYear, startDateMonth, startDateMonth);
			break;

		// Start Date
		case END_DATE_DIALOG_ID:
			dialog = new DatePickerDialog(this, endDateSelectListener,
					endDateYear, endDateMonth, endDateMonth);
			break;
		}

		return dialog;
	}
	*/
	@Override
	protected void onStop() {
		super.onStop();

		if (dao != null) {
			dao.close();
		}
	}
}